package com.hk.utils;


import java.sql.*;

/**
 * @Author: zhe
 * @Date: 2021/6/5 17:16
 * @Statement: Mysql数据库连接
 */
public class JdbcMysql {

    private static Connection connection = null;
    private static Statement statement = null;
    private static ResultSet resultSet = null;

    //    懒汉式单例模式
    private static JdbcMysql jdbcMysql;

    public static JdbcMysql getInstance() {
        if (jdbcMysql == null) {
            jdbcMysql = new JdbcMysql();
        }
        return jdbcMysql;
    }

    /**
     * 获取Connection进行事务提交
     *
     * @return
     */
    public Connection getConnection() {
        //Mysql8 .0 版本以下
        String mysql = "com.mysql.jdbc.Driver";
//        Mysql8.0以上
        String mysql8 = "com.mysql.cj.jdbc.Driver";
        String user = "root";
        String pwd = "123456";
        String url = "jdbc:mysql://127.0.0.1:3306/hk_company?useSSL=false&useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8";
        try {
//            1、加载驱动
            Class.forName(mysql8);
//            2、配置连接
            connection = DriverManager.getConnection(url, user, pwd);
            return connection;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    /**
     * 私有构建参数
     */
    private JdbcMysql() {
    }

    /**
     * JDBC连接Mysql数据源
     *
     * @return
     */
    private Statement jdbcMysql() {
//        Mysql8.0版本以下
        String mysql = "com.mysql.jdbc.Driver";
//        Mysql8.0以上
        String mysql8 = "com.mysql.cj.jdbc.Driver";
        String user = "root";
        String pwd = "123456";
        String url = "jdbc:mysql://127.0.0.1:3306/hk_company?useSSL=false&useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8";
        try {
//            1、加载驱动
            Class.forName(mysql8);
//            2、配置连接
            connection = DriverManager.getConnection(url, user, pwd);
//            3、获取prepareStatement对象 预编译 暂不使用
//            使用statement，直接赋值
            statement = connection.createStatement();
            return statement;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    /**
     * 查询数据
     *
     * @param sql
     * @return
     */
    public ResultSet jdbcQuery(String sql) {
        //            4、执行Sql语句
        statement = jdbcMysql();
        try {
            resultSet = statement.executeQuery(sql);
            return resultSet;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    /**
     * 增删改数据
     *
     * @param sql
     * @return
     */
    public int jdbcUpdate(String sql) {
        //            4、执行Sql语句
        statement = jdbcMysql();
        try {
//            返回修改了多少行，正常1
            int i = statement.executeUpdate(sql);
            return i;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }

    /**
     * 关闭资源
     */
    public static void jdbcClose() {
        try {
            if (resultSet != null) {
                resultSet.close();
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        try {
            if (statement != null) {
                statement.close();
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        try {
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

}
